package com.quickcancellation.crm.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.entity.Dictionary;
import com.quickcancellation.crm.mapper.DictionaryMapper;
import com.quickcancellation.crm.pojo.vo.DicKeyValVo;
import com.quickcancellation.crm.service.IDictionaryService;
import com.quickcancellation.crm.pojo.vo.DicResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-02-18
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public IPage<Dictionary> selectByDicList(PageParamsDto<Dictionary> params) {


        MPJQueryWrapper wrapper = new MPJQueryWrapper<DicResultVo>()
                .selectAll(DicResultVo.class)
                .select("t.id",
                        "t.dicKey",
                        "t.dicVal",
                        "t.dicModel",
                        "m.modelname as modelName")
                .leftJoin("sys_t_model m on m.modelid = t.dicModel");


        //列表查询
//        List<DicResultVo> list = dictionaryMapper.selectJoinList(DicResultVo.class, wrapper);

//                System.out.println(JSONObject.toJSONString(list));
        //分页查询 （需要启用 mybatis plus 分页插件）


        // 拼接参数
        if (StringUtils.hasLength(params.getParams().getDicKey())) {
            wrapper.like("dicKey", params.getParams().getDicKey());
        }

        if (StringUtils.hasLength(params.getParams().getDicVal())) {
            wrapper.like("dicVal", params.getParams().getDicVal());
        }

        if (null != params.getParams().getDicModel() && !"-1".equals(params.getParams().getDicModel())) {
            wrapper.eq("dicModel", params.getParams().getDicModel());
        }
//        }


        IPage iPage = dictionaryMapper.selectJoinPage(new Page<>(params.getPage(), params.getLimit()), DicResultVo.class, wrapper);

        System.out.println(JSONObject.toJSONString(iPage));
        return iPage;

    }

    @Override
    public String getNewDicKey() {
        return  dictionaryMapper.getNewKey().toString();
    }

    @Override
    public boolean existsDictionary(Long id, String dicVal) {

        // id非空 且 字典值如果没变就不需要做其他校验
        if (null != id && dictionaryMapper.selectById(id).getDicVal().equals(dicVal)) {
            return false;
        }

        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dicVal", dicVal);

        return dictionaryMapper.exists(queryWrapper);
    }

    @Override
    public List<DicKeyValVo> getDicKeyAndValByModel(String model) {
        return dictionaryMapper.getDicKeyAndValByModel(model);
    }
}
